home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1121 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.3 KB

  1. From: austern@isolde.mti.sgi.com (Matt Austern)
  2. Message-ID: <AUSTERN.96Apr16155031@isolde.mti.sgi.com>
  3. X-Original-Date: 16 Apr 1996 22:50:31 GMT
  4. Path: in1.uu.net!bounce-back
  5. Date: 17 Apr 96 08:47:22 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Better template syntax?
  9. Organization: SGI
  10. References: <31741E6C.53CA@cs.tu-berlin.de>
  11. Reply-To: austern@mti.mti.sgi.com
  12. In-Reply-To: Roman Lechtchinsky's message of 16 Apr 96 22:30:13 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBFAgUBMXSwNuEDnX0m9pzZAQF9AAF+Jh4rSysCBzeLpXyxeXkx/6Vjk+NpyFTb
  15.     Lh6uW9cpCFiFRz1ebdKzk5i4INU8xV2e
  16.     =TOwM
  17.  
  18. In article <31741E6C.53CA@cs.tu-berlin.de> Roman Lechtchinsky
  19. <wolfro@cs.tu-berlin.de> writes:
  20.  
  21. > sorry for being curious but I'm only human... My question is: when declaring
  22. > templates, why isn't it required to declare what one expects of the 
  23. > template's parameters? Let's create a template function:
  24. > template<class T> bool equal( const T& t1, const T& t2 )
  25. > {
  26. >   return t1==t2;
  27. > }
  28. > Now, personally I would prefer to write something like:
  29. > template<class T { bool operator==( const T& t ) const; }> ...
  30.  
  31. Specifying something like this correctly turns out to be extremely
  32. difficult: it's very hard to come up with syntax that neither
  33. overspecifies nor underspecifies the constraints.  In this particular
  34. case, for example: do you really care whether operator== takes a const
  35. T&, as opposed to a T?  Does it matter that it returns bool, instead
  36. of something (like int) that is freely convertable to bool?  Does it
  37. matter whether this is T::operator==(T), or ::operator==(T,T), or any
  38. of a dozen other minor variations?
  39.  
  40. More realistic cases are even harder to deal with.  Consider, for
  41. example, the for_each algorithm from STL:
  42. template <class InputIterator, class Function>
  43. Function for_each(InputIterator first, InputIterator last, Function f) {
  44.     while (first != last) f(*first++);
  45.     return f;
  46. }
  47.  
  48. The constraints are as follows:
  49.   There must be either a built-in operator!=, or a global operator!=,
  50.     or a member function InputIterator::operator!=, such that applying
  51.     operator!= to two InputIterators is well-defined and yields something
  52.     that's convertable to bool.
  53.   Operators ++ and * are defined such that *InputIterator++ yields an
  54.     rvalue of some type.
  55.   An object of type Function has an operator() that takes an argument
  56.     of the type returned by *InputIterator++, or at least a type to
  57.     which the return value of *InputIterator++ can be converted.
  58.     The return value of operator() is unimportant.
  59.  
  60. Notice what's tricky here: the important constraints are relationships
  61. between different types, some of which (like the return value of
  62. InputIterator++, and the return value of *InputIterator++) aren't
  63. named as template parameters.  This rapidly becomes a problem in 
  64. formal specification.
  65.  
  66. This issue is something that has been thought about, and there's some
  67. discussion of it in D&E.
  68. -- 
  69. Matt Austern
  70. SGI: MTI Compilers Group
  71. austern@isolde.mti.sgi.com
  72. ---
  73. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  74. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  75. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  76. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  77. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  78.